package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.ObjectHashMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Resource
    BaseCategoryViewMapper baseCategoryViewMapper;
    @Resource
    BaseAttributeMapper baseAttributeMapper;

    /**
     * 查询分类列表
     *
     * @return
     */
    @Override
    public Object getBaseCategoryList() {
        //查询全部分类识图：一级二级三级
        List<BaseCategoryView> baseCategory1ViewList = baseCategoryViewMapper.selectList(null);
        //一对多关系遍历 分桶 遍历
        return baseCategory1ViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id))
                .entrySet().stream().map(category1 -> {//遍历每个一级分类，获取一级下的二级三级分类
                    //返回结果
                    JSONObject result1 = new JSONObject();
                    //获取key
                    Long category1Id = category1.getKey();
                    result1.put("categoryId", category1Id);
                    //获取对应的二级三级的信息 二级重复
                    List<BaseCategoryView> baseCategory2ViewList = category1.getValue();
                    String category1Name = baseCategory2ViewList.get(0).getCategory1Name();
                    result1.put("categoryName", category1Name);

                    //根据二级分类再次分桶
                    List<JSONObject> category2List = baseCategory2ViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id))
                            .entrySet().stream().map(category2 -> {
                                //返回结果
                                JSONObject result2 = new JSONObject();
                                //获取二级分类key
                                Long category2Id = category2.getKey();
                                result2.put("categoryId", category2Id);
                                //获取二级分类下的三级分类
                                List<BaseCategoryView> category2Value = category2.getValue();
                                //获取二级分类名字
                                String category2Name = category2Value.get(0).getCategory3Name();
                                result2.put("categoryName", category2Name);
                                //获取三级具体分类
                                List<JSONObject> category3List = category2Value.stream().map(category3 -> {
                                    //返回结果
                                    JSONObject result3 = new JSONObject();
                                    //获取三级分类id
                                    Long category3Id = category3.getCategory3Id();
                                    result3.put("categoryId", category3Id);
                                    //获取三级分类名字
                                    String category3Name = category3.getCategory3Name();
                                    result3.put("categoryName", category3Name);
                                    return result3;
                                }).collect(Collectors.toList());
                                //保存二级分类的三级分类
                                result2.put("categoryChild", category3List);
                                return result2;
                            }).collect(Collectors.toList());
                    //保存一级分类的二级分类
                    result1.put("categoryChild", category2List);
                    return result1;
                }).collect(Collectors.toList());
//		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
//		return baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id)).entrySet().stream().map(category1 -> {
//			JSONObject object1 = new JSONObject();
//			Long key = category1.getKey();
//			object1.put("", key);
//			List<BaseCategoryView> category2ValueList = category1.getValue();
//			String category1Name = category2ValueList.get(0).getCategory1Name();
//			object1.put("", category1Name);
//			object1.put("", category2ValueList);
//			List<Object> list2 = category2ValueList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id)).entrySet().stream().map(category2 -> {
//				JSONObject object2 = new JSONObject();
//				Long category2Key = category2.getKey();
//				object2.put("", category2Key);
//				List<BaseCategoryView> category3ValueList = category2.getValue();
//				String category2Name = category3ValueList.get(0).getCategory2Name();
//				object2.put("", category2Name);
//				object2.put("", category3ValueList);
//				List<JSONObject> list3 = category3ValueList.stream().map(category3 -> {
//					JSONObject object3 = new JSONObject();
//					Long category3Id = category3.getCategory3Id();
//					object3.put("", category3Id);
//					String category3Name = category3.getCategory3Name();
//					object3.put("", category3Name);
//					return object3;
//				}).collect(Collectors.toList());
//				return object2;
//			}).collect(Collectors.toList());
//
//			return object1;
//		}).collect(Collectors.toList());

    }

    /**
     * 根据一级id查询标签
     *
     * @param category1Id
     * @return
     */
    @Override
    public Object findAttribute(Long category1Id) {
        return baseAttributeMapper.selectBaseAttrByCategory1Id(category1Id);
    }
}
